home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1 / Ian and Stuart's One (Australia).iso / Australasian Legends / Commercial / Rainbow Hill / MacDOS™ 2.0.0 / batches / uniqueName.bat < prev    next >
DOS Batch File  |  1994-06-30  |  3KB  |  88 lines

  1. @echo off
  2. !  uniqueName.bat    Changes an existing name and makes it unique
  3. !
  4. !  uniqueName name varName
  5. !
  6. !  where 'name' is the name to be made unique (possibly including a path) and 'varName'
  7. !  is a string containing the name of the global variable into which the unique file name
  8. !  is to be returned.
  9. !  For example: uniqueName "bla\whatever" "storeHere"
  10. !  sets the variable named "storeHere" to "whatever 2". If in the folder "bla" there is
  11. !  already an item (folder or file) named "whatever 2", uniqueName sets storeWhere to
  12. !  "whatever 3", etc.
  13. !
  14. !  If the length of the item name exceeds 31 after appending the sequence number, the
  15. !  name is shortened.
  16. !
  17. !  If the item does not exist, uniqueName sets 'varName' to the file name contained in 'name'.
  18. !
  19. !  uniqueName calls extractName
  20. !
  21. !  Copyright © 1994 by Rainbow Hill Pty Ltd. All rights reserved.
  22. !
  23.  
  24.     ! ensure that the first two parameters are there and that the third one is not
  25.     set doserr=13
  26.     if not "%3 " == " " goto ERR_LBL
  27.     if "%1 " == " " goto ERR_LBL
  28.     if "%2 " == " " goto ERR_LBL
  29.     onerror ERR_LBL
  30.  
  31.     ! separate path and item name
  32.     call extractName "%1" uniqueName_base
  33.     set uniqueName_folder=%WHERE%
  34.     if "%uniqueName_base%" == "%1" goto FOLDER_CHECKED_LBL
  35.     set uniqueName_folder=%1
  36.     decr uniqueName_folder by "%uniqueName_base%"
  37.     onerror SPLIT_DONE_LBL
  38.     decr uniqueName_folder by "\"
  39. :SPLIT_DONE_LBL
  40.     onerror ERR_LBL
  41.  
  42.     ! check that the folder exists
  43.     set doserr=27
  44.     if not existdir "%uniqueName_folder%" goto ERR_LBL
  45. :FOLDER_CHECKED_LBL
  46.  
  47.     ! if the item does not exist, return the name unchanged
  48.     if exist "%1" goto EXISTS_LBL
  49.     if existdir "%1" goto EXISTS_LBL
  50.     set %2=%uniqueName_base%
  51.     set doserr=0
  52.     goto DONE_LBL
  53.  
  54. :EXISTS_LBL
  55.     onerror X_EXIST_ERR_LBL
  56.  
  57. :TRY_NEWNAME_LBL
  58.     set uniqueName_seq=1
  59.  
  60. :TRY_NEWSEQ_LBL
  61.     set doserr=0
  62.     incr uniqueName_seq
  63.     set uniqueName_name=%uniqueName_base% %uniqueName_seq%
  64.     if exist "%uniqueName_folder%\%uniqueName_name%" goto TRY_NEWSEQ_LBL
  65.     if existdir "%uniqueName_folder%\%uniqueName_name%" goto TRY_NEWSEQ_LBL
  66.  
  67.     ! save the name into the variable specified by the caller
  68.     set %2=%uniqueName_name%
  69.     set doserr=0
  70.     goto DONE_LBL
  71.  
  72. :X_EXIST_ERR_LBL
  73.     ! if the name is too long shorten it, else abort the batch
  74.     if not %doserr% == 26 goto ERR_LBL
  75.     incr uniqueName_base by @
  76.     decr uniqueName_base by 2
  77.     goto TRY_NEWNAME_LBL
  78.  
  79. :ERR_LBL
  80.     show %doserr%
  81.  
  82. :DONE_LBL
  83.     ! remove the temporary variables
  84.     set uniqueName_name=
  85.     set uniqueName_base=
  86.     set uniqueName_seq=
  87.     set uniqueName_folder=
  88.